﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Database;
using CE.UserAccess;

public partial class User_UserAccess_reportsuser_account_unlock : System.Web.UI.Page
{
    string PageCode = "CXTHRUAS";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            LoadUserList();
            ddlUser_SelectedIndexChanged(sender, e);
        }
        if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
        {
            CheckPageAccess();
        }
    }

    protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnUnLock.Enabled = false;
        grvUserAccount.DataBind();
        if (grvUserAccount.Rows.Count > 0)
        {
            btnUnLock.Enabled = true;
        }
    }

    protected void grvUserAccount_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Int64 _UserStatusId = Convert.ToInt64(DataBinder.Eval(e.Row.DataItem, "UserStatusId"));
            bool _AccountStatus = Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "IsBlock"));
            chkLock.Checked = _AccountStatus;
            lblUserStatusId.Text = _UserStatusId.ToString();
        }
    }

    protected void btnUnLock_Click(object sender, EventArgs e)
    {
        if (ddlUser.Text != "0")
        {
            AccountStatusDB _accountstatusdb = new AccountStatusDB();
            Int64 _UserId = 0;
            Int64 _UserStatusId = 0;
            _UserStatusId = Convert.ToInt64(lblUserStatusId.Text);
            _UserId = Convert.ToInt64(ddlUser.Text);
            _accountstatusdb.UpdateStatus(_UserId, _UserStatusId, chkLock.Checked);
            grvUserAccount.DataBind();
        }
    }

    public string GetPassword(string AuthCode, string CEID)
    {
        Int64 LoginUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
        Int64 UserId = Convert.ToInt64(CEID);
        string Password = "XXXXXXXX";
        if (UserId == 1 && UserId == LoginUserId)
        {
            Password = AuthCode;
        }
        else if (UserId != 1 && LoginUserId == 1)
        {
            Password = AuthCode;
        }
        else if (UserId == LoginUserId)
        {
            Password = AuthCode;
        }
        return Password;
    }

    private void LoadUserList()
    {
        ddlUser.Items.Clear();
        MyDatabase _db = new MyDatabase();
        DataTable _dtUser = new DataTable();
        SortedList _sPara = new SortedList();
        _dtUser = _db.GetDataTableBySP("sprocCXTHUserSelectItemList", _sPara);

        if (_dtUser != null && _dtUser.Rows.Count > 0)
        {
            for (int i = 0; i < _dtUser.Rows.Count; i++)
            {
                ddlUser.Items.Add(new ListItem(_dtUser.Rows[i]["Email"].ToString(), _dtUser.Rows[i]["UserId"].ToString()));
            }
        }
        else
        {
            ddlUser.Items.Clear();
            ddlUser.Items.Insert(0, new ListItem("None", "0"));
        }
    }
   
    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
        else if (PageAccess > 0)
        {
            EnablePageAccess(PageAccess);
        }
    }

    private void EnablePageAccess(Int64 PageAccess)
    {
        if (PageAccess == 100)
        {
            btnUnLock.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnUnLock.Enabled = true;
        }
    }

}
